package com.itheima.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SmsUtil {
    public static void sendSmsCode(String mobile,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fcj9C9vQ1aVrArYffZT", "OJoLlgd2bZ88chI0sRpOuhnxqLJOW4");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", "传智健康");
        request.putQueryParameter("TemplateCode", "SMS_181852789");
        JSONObject param = new JSONObject();
        //put(String key, Object value)方法，在JSONObject对象中设置键值对在，在进行设值得时候，key是唯一的，如果用相同的key不断设值得时候，保留后面的值。
        param.put("code",code);
        //toJSONString() /toString():将JSONObject对象转换为json的字符串
        request.putQueryParameter("TemplateParam",param.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
